/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is this.
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file OcciTaskBase.cpp
 * @brief auto generated.
 * @author K.Ozaki
 * @date 2011/03/11
 */

#include "semi/OcciTaskBase.h"

namespace semi {

OcciTaskBase::OcciTaskBase() {
}

OcciTaskBase::~OcciTaskBase() {
}

Param OcciTaskBase::preProcess(Param params) {

    if (this->getLogger().isDebugEnabled())
        this->getLogger().debug("preProcess(occi task) of " + this->getTaskId()
                + " start.");

    Param ret = params;

    //    this->connection.init(this->getTaskId());
    //
    //    try {
    //        this->connection.connect();
    //    }
    //    // sql exception
    //    catch (SQLException sx) {
    //
    //        string msg = this->connection.loggingSqlException(sx);
    //        ret.success = false;
    //        ret.message = &msg;
    //
    //        this->connection.rollback();
    //        this->connection.terminate();
    //    }
    OcciConnection conn = OcciConnection::getConnection(this->getTaskId());

    if (!conn.isValid())
        this->getLogger().info("preProcess has not occi connection");

    ret = this->preProcess(conn, params);

    if (this->getLogger().isInfoEnabled())
        this->getLogger().info("preProcess(occi task) of " + this->getTaskId()
                + " end.");

    return ret;
}

Param OcciTaskBase::preProcess(OcciConnection conn, Param params) {
    if (this->getLogger().isDebugEnabled())
        this->getLogger().debug("preProcess(with db connection) of "
                + this->getTaskId() + " is empty.");
    return params;
}

Param OcciTaskBase::endProcess(Param params) {

    if (this->getLogger().isDebugEnabled())
        this->getLogger().debug("endProcess(occi task) of " + this->getTaskId()
                + " start.");

    //    Param ret = this->endProcess(this->connection, params);
    //    this->connection.terminate();

    OcciConnection conn = OcciConnection::getConnection(this->getTaskId());
    Param ret = this->endProcess(conn, params);
    conn.terminate();

    if (this->getLogger().isInfoEnabled())
        this->getLogger().info("preProcess(occi task) of " + this->getTaskId()
                + " end.");

    return ret;
}

Param OcciTaskBase::endProcess(OcciConnection conn, Param params) {
    if (this->getLogger().isDebugEnabled())
        this->getLogger().debug("endProcess(with db connection) of "
                + this->getTaskId() + " is empty.");
    return params;
}

Param OcciTaskBase::process(OcciConnection conn, Param params) {
    if (this->getLogger().isInfoEnabled())
        this->getLogger().info("process(with db connection) of "
                + this->getTaskId() + " is empty.");
    return params;
}

Param OcciTaskBase::avoidProcess(Param params) {
    if (this->getLogger().isInfoEnabled())
        this->getLogger().info("avoidProcess(occi task) of "
                + this->getTaskId() + " is empty.");
    return params;
}

Param OcciTaskBase::process(Param params) {

    Param ret = params;
    try {

        OcciConnection conn = OcciConnection::getConnection(this->getTaskId());

        if (conn.isValid()) {
            // this->getLogger().info("connection is valid almost.");
            ret = this->process(conn, params);
        }
        // connection is null.
        else {
            this->getLogger().info("connection is null.");
            ret = this->avoidProcess(params);
        }
    }
    // sql exception
    catch (SQLException sx) {

        //        // for retry
        //        if (!this->onRetry && this->Retry == retryCount)
        //            this->retryCount = 0;

        ret.success = false;
        //        string msg = OcciConnection::connection.loggingSqlException(sx);
        //        ret.message = &msg;

        ret = this->avoidProcess(params);
    }
    return ret;

}

}
